<template>
  <div>
    <!-- 正式环境去掉 -sandbox -->
    <form ref="formFace" class="form" style="overflow: hidden" action="https://iauth-sandbox.wecity.qq.com/new/cgi-bin/auth.php" method="post">
      <input type="hidden" name="appid" :value="appid">
      <input type="hidden" name="signature" :value="signature">
      <input type="hidden" name="redirect" :value="redirect">
      <input type="hidden" name="uid" :value="uid">
      <input type="hidden" name="type" value="0">
      <input type="hidden" name="sig" :value="sig">
      <div  class="register" style="width: 100%;" @click="sweepInto">住建局实名认证测试</div>
    </form>
  </div>
</template>

<script>
import crypto from 'crypto'
export default {
  data () {
    return {
      uid: '1',
      appid: '4466',
      secretkey: 'eff91ab3b61167f468a2478c94019885',
      aeskey: 'c165db065206659ca3b343829dc4d261',
      redirect: '',
      signature: null,
      sig: null
    }
  },
  methods: {
    sweepInto () {
      // 更新签名和校验
      this.signature = this.getAppSign()
      this.sig = this.getHashSig()
      this.$nextTick(() => {
        this.$refs.formFace.submit()
      })
    },
    // 获取认证签名
    getAppSign () {
      let appid = this.appid
      let secretkey = this.secretkey
      let apiname = 'auth'
      let e = 600
      let time = (Date.parse(new Date()) / 1000).toString()
      let orignal = 'a=' + appid + '&m=' + apiname + '&t=' + time + '&e=' + e
      orignal = new Buffer(orignal, 'utf8')
      let wordArray = crypto.createHmac('sha1', secretkey).update(orignal).digest()
      let buf = Buffer.concat([wordArray, orignal])
      let signature = buf.toString('base64')
      // console.log(signature)
      return signature
    },
    // 获取认证签名校验
    getHashSig () {
      let datas = {
        appid: this.appid,
        signature: this.signature,
        redirect: this.redirect,
        uid: '1',
        type: 0,
        sig: this.aeskey
      }
      let srcData = ''
      for (let index in datas) {
        if (index !== 'sig') {
          srcData += datas[index] + '-'
        }
      }
      srcData += 'authkey'
      return crypto.createHash('md5').update(srcData).digest('hex')
    }
  },
  created () {
    this.redirect = window.location.href.split('#')[0] + '#/test/shualianinfo'
  }
}
</script>

<style lang="css" scoped>
.form{ padding: 10px; }
.register{
  text-align: center; background: #000; color: #fff; font-size: 18px; line-height: 40px;
}
</style>
